package com.adnaan.restaurant.mapper;

import com.adnaan.restaurant.entity.ResShoppingCart;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ResShoppingCartMapper {

    @Select("select sc.id, sc.dishes_id, sc.quantity, sc.user_id, sc.shop_id, d.name as dishesName, d.pic as dishesPic, d.price as dishesPrice from res_shopping_cart sc left join res_dishes d on sc.dishes_id=d.id where sc.dishes_id = #{did} and sc.user_id=#{uid}")
    public ResShoppingCart selectShoppingCartByDishesId(@Param("uid") Long userId, @Param("did") Long dishesId);

    @Update("update res_shopping_cart set quantity = #{newQ}, update_time = now() where id = #{cid}")
    int updateShoppingCartNum(@Param("cid") Long id, @Param("newQ") Integer newQuantity);

    @Insert("insert into res_shopping_cart(dishes_id, quantity,user_id, shop_id, create_time) " +
            "VALUES (#{dishesId},#{quantity},#{userId},#{shopId},now())")
    int insertShoppingCart(ResShoppingCart resShoppingCart);

    @Select("select sc.id, sc.dishes_id, sc.quantity, sc.user_id, sc.shop_id, d.name as dishesName, d.pic as dishesPic, d.price as dishesPrice from res_shopping_cart sc left join res_dishes d on sc.dishes_id=d.id where sc.user_id = #{uid} and sc.shop_id = #{sid}")
    List<ResShoppingCart> findAll(@Param("uid") Long userId, @Param("sid") Long shopId);

    @Delete("delete from res_shopping_cart where id = #{delId}")
    int delCartDishes(@Param("delId") Long id);

    @Delete("delete from res_shopping_cart where user_id = #{uid} and shop_id=#{sid}")
    int clearCart(@Param("uid") Long userId, @Param("sid") Long shopId);

    @Update("update res_shopping_cart set quantity = #{newQ}, update_time = now() where id = #{cid}")
    int updateNum(@Param("cid") Long id, @Param("newQ") Integer newQuantity);
}
